package admin.controller;

import admin.bean.BootStrapExpressData;
import admin.bean.Express;
import admin.bean.Message;
import admin.bean.ResultData;
import exception.ReCodeException;
import mvc.ResponseBody;
import admin.service.ExpressService;
import admin.service.impl.ExpressServiceImpl;
import util.DateFormatUtil;
import util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ExpressController {
    ExpressService service = new ExpressServiceImpl();

    @ResponseBody("/AdminExpress/views/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){

        List<Map<String,Integer>> data = service.console();
        Message msg = new Message();
        if (data.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JsonUtil.toJSON(msg);

        return json;
    }

    @ResponseBody("/AdminExpress/views/express/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){

        //获取查询数据的起始索引值
        //int offset = Integer.parseInt(req.getParameter("offset")==null?"1":req.getParameter("offset"));
        int offset = Integer.parseInt(req.getParameter("offset"));
        //获取每页要查询的数据条数
        //int pageNumber = Integer.parseInt(req.getParameter("pageNumber")==null?"5":req.getParameter("pageNumber"));
        int pageNumber = Integer.parseInt(req.getParameter("pageNumber"));
        //PageUtil page = new PageUtil(offset,pageNumber);
        //进行查询
        List<Express> list = service.findAll(true,offset,pageNumber);
        List<BootStrapExpressData> list1 = new ArrayList<>();
        for(Express e:list) {
            String inTime = DateFormatUtil.format(e.getInTime());
            String outTime = e.getOutTime()==null?"未出库":DateFormatUtil.format(e.getOutTime());
            String status = e.getStatus()==0?"待取件":"已取件";
            String code = e.getCode()==null?"取件码已清空":e.getCode();
            BootStrapExpressData e2 = new BootStrapExpressData(e.getId(),e.getNumber(),e.getUserName(),e.getUserPhone(),e.getCompany(),code,inTime,outTime,status,e.getSysPhone());

            list1.add(e2);

        }
        List<Map<String,Integer>> console = service.console();
        Integer total = console.get(0).get("data1_num");
        //将集合封装为bootstrap-table识别的格式
        ResultData<BootStrapExpressData> data = new ResultData<>();
        data.setRows(list1);
        data.setTotal(total);
        String json = JsonUtil.toJSON(data);

        return json;
    }



    @ResponseBody("/AdminExpress/views/express/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp) throws ReCodeException {
        //获取前端传递过来的页面参数
        String number = req.getParameter("number");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String company = req.getParameter("company");
        
        //自定义录入人手机号
        String sysPhone = "15180787286";
        Express newExpress = new Express(number, userName, userPhone, company, sysPhone);

        boolean flag = service.insert(newExpress);
        Message msg = new Message();
        if (flag){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递录入成功");
            System.out.println("快递录入成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("快递录入失败");
            System.out.println("快递录入失败");
        }
        String json = JsonUtil.toJSON(msg);

        return json;
    }

    @ResponseBody("/AdminExpress/views/express/findByNumber.do")
    public String findByNumber(HttpServletRequest req, HttpServletResponse resp){
        //接收前端参数
        String number = req.getParameter("number");
        Express express = service.findByNumber(number);
        Message msg = new Message();
        if (express == null){
            msg.setStatus(-1);
            msg.setResult("单号不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("快递信息查询成功");
            msg.setData(express);
        }
        String json = JsonUtil.toJSON(msg);

        return json;
    }

    @ResponseBody("/AdminExpress/views/express/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp) throws ReCodeException {
        //接收前端页面参数
        int id = Integer.parseInt(req.getParameter("id"));
        String number = req.getParameter("number");
        String userName = req.getParameter("userName");
        String userPhone = req.getParameter("userPhone");
        String company = req.getParameter("company");

        int status = Integer.parseInt(req.getParameter("status"));

        Express newExpress = new Express();
        newExpress.setId(id);
        newExpress.setNumber(number);
        newExpress.setUserName(userName);
        newExpress.setUserPhone(userPhone);
        newExpress.setCompany(company);
        newExpress.setStatus(status);

        //调用service
        boolean flag = service.update(id,newExpress);
        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }

        String json = JsonUtil.toJSON(msg);

        return json;
    }

    @ResponseBody("/AdminExpress/views/express/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        //获取页面差数
        int id = Integer.parseInt(req.getParameter("id"));

        //调用service
        boolean flag = service.delete(id);

        Message msg = new Message();
        if (flag){
            msg.setStatus(0);
            msg.setResult("删除成功");
            System.out.println("快件信息删除成功");
        }else {
            msg.setStatus(-1);
            msg.setResult("删除失败");
            System.out.println("快件信息删除成功");
        }

        String json = JsonUtil.toJSON(msg);



        return json;
    }

}
